package com.stormrage.edu.serv.exam.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.stormrage.constants.ErrCons;
import com.stormrage.edu.dao.exam.ExmExamDao;
import com.stormrage.edu.serv.edu.EduModuleServ;
import com.stormrage.edu.serv.exam.ExmAnswerServ;
import com.stormrage.edu.serv.exam.ExmExamServ;
import com.stormrage.edu.serv.exam.ExmRelateCountServ;
import com.stormrage.edu.serv.learn.LeaStudentCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.edu.serv.learn.LeaTermServ;
import com.stormrage.entity.edu.EduModule;
import com.stormrage.entity.exam.ExmAnswer;
import com.stormrage.entity.exam.ExmRelateCount;
import com.stormrage.entity.exam.rep.ExmExamListRep;
import com.stormrage.entity.exam.rep.ExmExamRep;
import com.stormrage.entity.exam.req.ExmExamReq;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.LeaTerm;
import com.stormrage.entity.learn.req.LeaCourseStudyReq;
import com.stormrage.entity.learn.req.LeaStudentCourseReq;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormrage.entity.learn.view.LeaStudentCourse;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.NumberUtil;

/**
 * @Title: ExmExamServImpl.java
 * @Package com.stormrage.edu.serv.exam.impl
 * @Description: 考试试题 Service
 * @author www.stormragetech.com
 * @date 2016年5月16日
 * @version V1.0
 */
@Service
public class ExmExamServImpl implements ExmExamServ {

	private static final Logger logger = LoggerFactory.getLogger(ExmExamServImpl.class);

	@Autowired
	private EduModuleServ eduModuleServ;

	@Autowired
	private ExmExamDao exmExamDao;

	@Autowired
	private ExmAnswerServ exmAnswerServ;

	@Autowired
	private ExmRelateCountServ exmRelateCountServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Autowired
	private LeaStudentCourseServ leaStudentCourseServ;

	@Autowired
	private LeaTermServ leaTermServ;

	@Override
	public List<ExmExamRep> queryListForCourse(final LeaStudentReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListForCourse 】-【 STARTED 】");

		LeaTerm term = leaTermServ.queryById(arg0.getTermId());
		arg0.setMajorId(term.getMajorId());

		LeaStudent stu = leaStudentServ.queryByAcctMajor(arg0);

		LeaStudentCourseReq req = LeaStudentCourseReq.instance(stu.getId(), 0);
		List<LeaStudentCourse> array = leaStudentCourseServ.queryListByStuNess(req);

		List<ExmExamRep> list = new ArrayList<ExmExamRep>();

		List<ExmExamRep> sele = new ArrayList<ExmExamRep>();
		List<ExmExamRep> mult = new ArrayList<ExmExamRep>();
		List<ExmExamRep> corr = new ArrayList<ExmExamRep>();

		Iterator<LeaStudentCourse> it = array.iterator();
		while (it.hasNext()) {
			LeaStudentCourse course = it.next();
			ExmExamReq examReq = ExmExamReq.instance(arg0.getTermId(), course.getCourseId(), 0, 0);
			logger.debug(" req为 " + examReq.toString());

			ExmExamListRep rep = queryListRepByRelate(examReq);
			sele.addAll(rep.getSele());
			mult.addAll(rep.getMult());
			corr.addAll(rep.getCorr());
		}

		Collections.shuffle(sele);
		Collections.shuffle(mult);
		Collections.shuffle(corr);

		list.addAll(sele);
		list.addAll(mult);
		list.addAll(corr);

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListForCourse 】-【 COMPLETED 】");
		return list;

	}

	@Override
	public List<ExmExamRep> queryListForModule(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListForModule 】-【 STARTED 】");

		LeaCourseStudyReq req = new LeaCourseStudyReq(null, arg0, 0);
		List<EduModule> array = eduModuleServ.queryListByMajorNess(req);

		List<ExmExamRep> list = new ArrayList<ExmExamRep>();

		List<ExmExamRep> sele = new ArrayList<ExmExamRep>();
		List<ExmExamRep> mult = new ArrayList<ExmExamRep>();
		List<ExmExamRep> corr = new ArrayList<ExmExamRep>();

		Iterator<EduModule> it = array.iterator();
		while (it.hasNext()) {
			EduModule module = it.next();
			ExmExamReq examReq = ExmExamReq.instance(arg0, module.getId(), 1, 0);
			ExmExamListRep rep = queryListRepByRelate(examReq);
			sele.addAll(rep.getSele());
			mult.addAll(rep.getMult());
			corr.addAll(rep.getCorr());
		}

		Collections.shuffle(sele);
		Collections.shuffle(mult);
		Collections.shuffle(corr);

		list.addAll(sele);
		list.addAll(mult);
		list.addAll(corr);

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListForModule 】-【 COMPLETED 】");
		return list;

	}

	@Override
	public ExmExamListRep queryListRepByRelate(final ExmExamReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListByRelate 】-【 STARTED 】");

		ExmRelateCount relateCount = exmRelateCountServ.queryByTermRelateType(arg0);

		ExmExamReq req = ExmExamReq.instance(arg0, 1, relateCount.getSele());
		List<ExmExamRep> sele = randomRepList(req);

		req = ExmExamReq.instance(arg0, 2, relateCount.getSele());
		List<ExmExamRep> mult = randomRepList(req);

		req = ExmExamReq.instance(arg0, 3, relateCount.getSele());
		List<ExmExamRep> corr = randomRepList(req);

		ExmExamListRep entity = ExmExamListRep.instance(sele, mult, corr);

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 queryListByRelate 】-【 COMPLETED 】");
		return entity;

	}

	private List<ExmExamRep> randomRepList(final ExmExamReq arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 randomRepList 】-【 STARTED 】");

		logger.debug(" ExmExamReq为 " + arg0.toString());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("termId", arg0.getTermId());
		map.put("relateType", arg0.getRelateType());
		map.put("relateId", arg0.getRelateId());
		map.put("type", arg0.getType());

		List<ExmExamRep> array = exmExamDao.queryListByTypeRelate(map);

		if (array.size() < arg0.getCount())
			throw StormrageTechException.instance(ErrCons.SERV_ERR, "生成考题发生错误【 1 】", StormrageTechException.ERR_APP,
					"【 ExmExamServ 】- 【 randomRepList 】");

		List<ExmExamRep> list = new ArrayList<ExmExamRep>();
		HashSet<Integer> set = new HashSet<Integer>();
		NumberUtil.randomSet(0, array.size(), arg0.getCount(), set);
		int seq = 0;

		Iterator<Integer> it = set.iterator();
		while (it.hasNext()) {

			Integer index = it.next();
			ExmExamRep entity = array.get(index);

			List<ExmAnswer> answer = exmAnswerServ.queryListByExamId(entity.getId());

			logger.debug(" 考试 " + array.size() + " || " + arg0.getCount());

			if (array.size() < arg0.getCount())
				throw StormrageTechException.instance(ErrCons.SERV_ERR, "生成考题发生错误【 2 】", StormrageTechException.ERR_APP,
						"【 ExmExamServ 】- 【 randomRepList 】");

			entity = ExmExamRep.instance(entity, seq, answer);
			list.add(entity);
			seq++;
		}

		logger.debug(" 【 Serv 】-【 ExmExamServ 】-【 randomRepList 】-【 COMPLETED 】");
		return list;

	}

}
